package net.alteiar.images.filter.elements;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import net.alteiar.Array2D;
import net.alteiar.images.filter.BasicFilter;
import net.alteiar.images.metadata.IImageInfo;
import net.alteiar.images.metadata.VisionInfo;

public class CompleteVisionFilter extends BasicFilter {
	public static int DO_NOT_CHANGE = 1;
	public static int HIDE = 2;
	public static int SHOW = 3;

	private static Color HIDDEN_DEFAULT = new Color(0f, 0f, 0f, 0.8f);
	private static Color VISIBLE_DEFAULT = new Color(0f, 0f, 0f, 0f);

	private final List<VisionInfo> vision;

	public CompleteVisionFilter(IImageInfo img) {
		this(img, HIDDEN_DEFAULT, VISIBLE_DEFAULT);
	}

	public CompleteVisionFilter(IImageInfo info, Color hidden, Color visible) {
		super(info);

		vision = new ArrayList<>();

		updateFilter();
	}

	public void addVision(VisionInfo builder) {
		vision.add(builder);
		builder.addObserver(this);
		updateFilter();
	}

	private void computeSquare(Array2D image, int squareSize, int x, int y,
			int value) {
		int realX = x * squareSize;
		int realY = y * squareSize;

		for (int k = 0; k < squareSize; ++k) {
			for (int k1 = 0; k1 < squareSize; ++k1) {
				image.set(realX + k, realY + k1, value);
			}
		}
	}

	@Override
	protected void updateFilter() {
		Array2D image = getImageArray2D();
		image.fill(HIDDEN_DEFAULT.getRGB());

		for (VisionInfo builder : vision) {
			builder.computeVision(image, VISIBLE_DEFAULT.getRGB(),
					HIDDEN_DEFAULT.getRGB());
		}

		for (int i = 0; i < info.getWidthSquare(); i++) {
			for (int j = 0; j < info.getHeightSquare(); j++) {
				int value = info.getValue(i, j);
				int color = -1;
				if (value == HIDE) {
					color = new Color(0.3f, 0f, 0f, 0.8f).getRGB();
				} else if (value == SHOW) {
					color = VISIBLE_DEFAULT.getRGB();
				}

				if (value != DO_NOT_CHANGE) {
					computeSquare(image, info.getSquareSize(), i, j, color);
				}
			}
		}
	}
}
